package com.gtvtv.sparkdesksdk.model.request.agent;

import java.util.List;
import java.util.Map;

/**
 * 工作流执行请求封装
 */
public class AgentRequest {
    private final AgentRequestHeader header;
    private final AgentRequestBody body;

    public AgentRequest(AgentRequestHeader header, AgentRequestBody body) {
        this.header = header;
        this.body = body;
    }

    public Map<String, String> getHeaders() {
        return header.toMap();
    }

    public AgentRequestBody getBody() {
        return body;
    }

    public void validate() throws IllegalArgumentException {
        List<AgentRequestHistoryMessageObject> history = body.getHistory();
        if (history != null && !history.isEmpty()) {
            for (int i = 0; i < history.size(); i++) {
                String expectedRole = (i % 2 == 0) ? "user" : "assistant";
                String actualRole = history.get(i).getRole();

                if (!expectedRole.equals(actualRole)) {
                    throw new IllegalArgumentException(
                            "历史消息顺序错误。位置 " + i + " 应为 '" + expectedRole +
                                    "', 实际为 '" + actualRole + "'"
                    );
                }
            }
        }
    }
}